package com.smartlink.action;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.pojo.Setting;
import com.smartlink.service.SettingService;

public class SettingAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private SettingService settingService;
	private String settingId;
	private String clsroomId;

	public String getClsroomId() {
		return clsroomId;
	}

	public void setClsroomId(String clsroomId) {
		this.clsroomId = clsroomId;
	}

	public String getSettingId() {
		return settingId;
	}

	public void setSettingId(String settingId) {
		this.settingId = settingId;
	}

	public void setSettingService(SettingService settingService) {
		this.settingService = settingService;
	}


	/**
	 * 更新设置
	 * 
	 * @return
	 */
	public String updateSetting() {
		HttpServletRequest request = ServletActionContext.getRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		String isAlarm = request.getParameter("isAlarm");
		String isAiRegulate = request.getParameter("isAiRegulate");
		String alarmBeginDate = request.getParameter("alarmBeginDate");
		String alarmOverDate = request.getParameter("alarmOverDate");
		String alarmPhone = request.getParameter("alarmPhone");
		String alarmEmail = request.getParameter("alarmEmail");
		String roomTemperature = request.getParameter("roomTemperature");
		String roomRh = request.getParameter("roomRh");
		String soilRh = request.getParameter("soilRh");
		Setting setting = settingService.getSettingById(settingId);

		try {
			if (alarmBeginDate != null && !"".equals(alarmBeginDate.trim())) {
				setting.setAlarmBeginDate(sdf.parse(alarmBeginDate));
			}

			if (alarmOverDate != null && !"".equals(alarmOverDate.trim())) {
				setting.setAlarmOverDate(sdf.parse(alarmOverDate));
			}

			if (alarmPhone != null && !"".equals(alarmPhone.trim())) {
				setting.setAlarmPhone(alarmPhone);
			}
			if (alarmEmail != null && !"".equals(alarmEmail.trim())) {
				setting.setAlarmEmail(alarmEmail);
			}
			if (roomTemperature != null && !"".equals(roomTemperature.trim())) {
				double temperature = Double.parseDouble(roomTemperature);
				setting.setRoomTemperature(temperature);

			}
			if (roomRh != null && !"".equals(roomRh.trim())) {
				double rh = Double.parseDouble(roomRh);
				setting.setRoomRh(rh);
				;
			}
			if (soilRh != null && !"".equals(soilRh.trim())) {
				double rh = Double.parseDouble(roomRh);
				setting.setSoilRh(rh);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		setting.setIsAlarm(Integer.parseInt(isAlarm));
		setting.setIsAiRegulate(Integer.parseInt(isAiRegulate));
		settingService.update(setting);
		return "SUCCESS";

	}

}
